<template>
  <div>
    <el-dialog
      title="关联列表" :visible.sync="templateDialog" height="450" width="80%" @open="onSearch()" @close="cancelDialog()">
      <el-container>
        <el-header style="height: auto">
          <el-form ref="queryForm" :inline="true" :model="query" class="demo-form-inline">
            <el-form-item label="范本编号">
              <el-input v-model="query.projectId" placeholder="请输入范本编号" />
            </el-form-item>
            <el-form-item label="范本名称">
              <el-input v-model="query.projectName" placeholder="请输入范本名称" />
            </el-form-item>
            <el-form-item label="版本">
              <el-input v-model="query.year" placeholder="请输入版本" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSearch()">搜索</el-button>
              <el-button @click="searchReset()">重置</el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main>
          <el-table ref="projectTable" :data="data" row-key="projectId" height="300" style="width: 100%">
            <el-table-column prop="shopType" label="范本编号"></el-table-column>
            <el-table-column prop="projectName" label="范本名称"></el-table-column>
            <el-table-column prop="projectName" label="版本"></el-table-column>
            <el-table-column prop="projectName" label="生效日期"></el-table-column>
            <el-table-column prop="projectName" label="说明"></el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="relateClick(scope.row)">选择</el-button>
                <el-button type="text" size="small" @click="viewClick(scope.row)">查看</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
          </el-pagination>
        </el-main>
        <el-footer align="middle">

        </el-footer>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'chose-project',
  props: {
    templateDialog: Boolean,
    projectId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      query: {
        userId: null,
        username: '',
        phone: '',
      },
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      relateData: {},
    }
  },
  methods: {
    // 确认
    confirmDialog() {
      this.$emit('setValue', this.relateData)
      this.relateData = {}
      this.cancelDialog()
    },
    // 关闭
    cancelDialog() {
      this.$refs['queryForm'].resetFields()
      this.$emit('closeDialog', false)
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page, this.query);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    relateClick(val) {
      this.relateData = val;
      this.confirmDialog();
    },
    viewClick() {}
  }
}
</script>

<style>

</style>
